package com.niodata.dp.task.dao;

import com.niodata.dp.task.entity.TaskConf;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by ndp on 17-8-17.
 */
@Mapper
public interface TaskConfMapper {

  @Insert("insert into TASK_CONF_MANAGER (ID,CONF_NAME,CONF_VALUE,DESCRIPTION,OWNER_TYPE,"
      + "OWNER_NAME,OWNER_ID) values (#{conf.id},#{conf.name},#{conf.params},"
      + "#{conf.desc},#{conf.ownerType},#{conf.ownerName},#{conf.ownerId})")
  public void saveTaskConf(@Param("conf") TaskConf taskConfManager);

  @Update("update TASK_CONF_MANAGER set CONF_NAME=#{conf.name},CONF_VALUE = #{conf.params},"
      + "DESCRIPTION=#{conf.desc} where ID = #{conf.id}")
  public void updateTaskConf(@Param("conf") TaskConf taskConfManager);

  @Delete("delete from TASK_CONF_MANAGER WHERE ID=#{id}")
  public void deleteTaskConf(@Param("id") String id);

  @Delete("delete from TASK_CONF_MANAGER WHERE CONF_NAME=#{name}")
  public void deleteTaskConfByName(@Param("name") String name);

  @Delete("delete from TASK_CONF_MANAGER WHERE CONF_NAME=#{name}")
  public TaskConf getTaskConfByName(@Param("name") String name);

  @Select("select * from TASK_CONF_MANAGER WHERE ID=#{id}")
  @ResultMap("taskConfResultMap")
  public TaskConf getTaskConfById(@Param("id") String id);

  /**
   * 查询用户可见的全部task，包括个人、组和公开的.
   *
   * @param userId userid
   * @param ids ids
   * @param keyWord name or desc contains key word
   * @param offset offset
   * @param count limit count
   * @return list
   */
  public List<TaskConf> findUserAuthorizedTaskConf(@Param("userId") Long userId,
      @Param("ids") List<Long> ids,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count);

  /**
   * 查询用户自己的任务.
   *
   * @param userId userid
   * @param keyWord name or desc contains key word
   * @param offset offset
   * @param count limit count
   * @return list
   */
  public List<TaskConf> findUserTaskConf(@Param("userId") Long userId,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count);

  /**
   * 查询某个组下可见的task.
   *
   * @param userId userid
   * @param groupId groupid
   * @param keyWord name or desc contains key word
   * @param offset offset
   * @param count count limit
   * @return list
   */
  public List<TaskConf> findUserAuthorizedGroupTaskConf(@Param("userId") Long userId,
      @Param("groupId") Long groupId,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count);
}
